<template>
  <el-form
    label-position="right"
    label-width="110px"
    :model="form"
    :rules="rules"
    ref="appBtnFrom"
  >
    <el-form-item
      label="功能名称"
      prop="name"
    >
      <el-input
        v-model="form.name"
        placeholder="请输入功能名称"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="功能权限值"
      prop="authorityName"
    >
      <el-input
        v-model="form.authorityName"
        placeholder="应用名称:菜单名称:功能权限值，如系统管理应用用户管理菜单新增用户（systemManage:userManage:add）"
      ></el-input>
    </el-form-item>
  </el-form>
</template>

<script>
import { createAppAuthNode, updateAppAuthNode } from '@/api/authority';
export default {
  name: 'AppBtnContent',
  inject: ['parent'],
  props: {
    btnInfo: {
      type: Object,
      required: true
    },
    type: {
      type: Number,
      required: true
    },
    // 上级菜单数据
    superiorMenuData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set() {
        this.$emit('update:visible', false);
      }
    }
  },
  data() {
    return {
      form: {
        name: '',
        authorityName: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入功能名称',
            trigger: 'blur'
          }
        ],
        authorityName: [
          {
            required: true,
            message: '请输入功能权限值',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  created() {
    if (this.type === 2) {
      // 修改
      let { function_name: name, auth_name: authorityName } = this.btnInfo;
      this.form = {
        name,
        authorityName,
        type: 1
      };
    }
  },
  methods: {
    // 取消
    cancelFn(formName) {
      this.$refs[formName].resetFields();
    },
    // 确认
    sureFn(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitFn();
        } else {
          return false;
        }
      });
    },
    // 提交
    submitFn() {
      let { name, authorityName } = this.form;
      let { id, service_id, service_name } = this.superiorMenuData;

      let btnParams = {
        function_name: name,
        auth_name: authorityName,
        pid: id,
        service_id,
        service_name
      };

      if (this.type === 1) {
        // 新增功能
        createAppAuthNode(btnParams).then(res => {
          this.operateFn(res);
        });
      } else {
        // 修改
        btnParams.id = this.btnInfo.id;
        // 仅功能
        updateAppAuthNode(btnParams).then(res => {
          this.operateFn(res);
        });
      }
    },
    // 操作
    operateFn(res) {
      this.$toast(res.message);
      this.$store.commit('appMenu/setAppTimestamp', new Date().getTime());
      this.$emit('closeFn');
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
  padding-bottom: 15px;
}

::v-deep .el-form-item {
  &:last-of-type {
    margin-bottom: 0;
  }
}
</style>
